package com.xhh.enjoyapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xhh.enjoyapp.Bean.Post;
import com.xhh.enjoyapp.R;
import com.xhh.enjoyapp.activity.Login;
import com.xhh.enjoyapp.activity.Receive;
import com.xhh.enjoyapp.constant.CommonConstant;

import java.util.List;

import cn.bmob.v3.BmobUser;

/**
 * @description 我的帖子适配器
 * @author: weiXhh
 * @create: 2020-05-15 17:21
 **/
public class MyPushInvitationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<Post> data;

    private int maxNum = 15;
    private Boolean isFootView = true; //是否footview

    public MyPushInvitationAdapter(Context context, List<Post> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_push_invitation_rv_item,parent,false);
        View footView = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_recycle_view_foot_item, parent, false);
        if(viewType == CommonConstant.F_TYPE){
            return new MyPushInvitationAdapter.RecyclerViewHolder(footView, CommonConstant.F_TYPE);
        } else {
            return new MyPushInvitationAdapter.RecyclerViewHolder(view, CommonConstant.N_TYPE);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(isFootView && getItemViewType(position) == CommonConstant.F_TYPE){
            final MyPushInvitationAdapter.RecyclerViewHolder recyclerViewHolder = (MyPushInvitationAdapter.RecyclerViewHolder) holder;
            recyclerViewHolder.loading.setText("加载中...");
            // 通知
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    maxNum += 8;
                    notifyDataSetChanged();
                }
            }, 2000);
        } else {
            // 加载的内容
            final MyPushInvitationAdapter.RecyclerViewHolder recyclerViewHolder = (MyPushInvitationAdapter.RecyclerViewHolder) holder;
            Post post = data.get(position);

            recyclerViewHolder.username.setText(post.getName());
            recyclerViewHolder.nickname.setText(post.getNickname());
            recyclerViewHolder.content.setText(post.getContent());
            recyclerViewHolder.time.setText(post.getCreatedAt());

            recyclerViewHolder.itemView.setOnClickListener(click->{
                int i = recyclerViewHolder.getAdapterPosition();
                // 已经登录,可点击查看内容
                if(BmobUser.getCurrentUser(BmobUser.class) != null){
                    // TODO 需要改动
                    Intent intent = new Intent(context, Receive.class);

                    intent.putExtra("id", data.get(i).getObjectId());
                    intent.putExtra("username", post.getName());
                    intent.putExtra("content", post.getContent());
                    intent.putExtra("time", post.getCreatedAt());
                    context.startActivity(intent);
                } else {
                    Toast.makeText(context, "请登录", Toast.LENGTH_SHORT).show();
                    context.startActivity(new Intent(context, Login.class));
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if(data.size() < maxNum){
            return data.size();
        }
        return maxNum;
    }

    @Override
    public int getItemViewType(int position) {
        if(position == (maxNum - 1)){
            return CommonConstant.F_TYPE;
        } else {
            return CommonConstant.N_TYPE;
        }
    }

    // 优化组件加载
    private class RecyclerViewHolder extends RecyclerView.ViewHolder {

        public TextView nickname;
        public TextView content;
        public TextView time;
        public TextView loading;
        public TextView username;

        public RecyclerViewHolder(View itemView, int viewType) {
            super(itemView);

            if(viewType == CommonConstant.N_TYPE){
                nickname = itemView.findViewById(R.id.my_push_invitation_nickname);
                content = itemView.findViewById(R.id.my_push_invitation_content);
                time = itemView.findViewById(R.id.my_push_invitation_time);
                username = itemView.findViewById(R.id.my_push_invitation_username);
            } else if(viewType == CommonConstant.F_TYPE){
                loading = itemView.findViewById(R.id.home_fragment_foot_item);
            }
        }
    }
}
